﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// FileDelete 的摘要说明
/// </summary>
public class FileDelete
{
    string content = string.Empty;//选择要删除的内容
    string type = string.Empty;//选择要删除的文件类型
    
    public FileDelete()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }    

    public FileDelete(string filecontent,string filetype)
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
        content = filecontent;
        type = filetype;
    }

    public void clearFolder(string path)
    {
        string fold_url = path;
        fold_url = System.Web.HttpContext.Current.Server.MapPath(fold_url);
        foreach (string filepath in Directory.GetFiles(fold_url))
        {
            FileInfo fi = new FileInfo(filepath);
            fi.Delete();
        }
    }

    public void deleteFile(string file_url)
    {
        try
        {
            if (file_url == "")
            {
                return;
            }
            string filePath = System.Web.HttpContext.Current.Server.MapPath(file_url);
            FileInfo fi = new FileInfo(filePath);
            fi.Delete();
        }
        catch (Exception ex)
        {
            return;
        }
    }

    public void file_delete()
    {
        switch (type)
        {
            case "image": imagefile_delete(); break;
            case "pdf": pdffile_delete(); break;
            case "data": break;
            case "movie": moviefile_delete(); break;
            default: break;
        }
    }

    private void imagefile_delete()
    {
        string fold_url = "files/" + content;
        fold_url = System.Web.HttpContext.Current.Server.MapPath(fold_url);
        foreach (string filepath in Directory.GetFiles(fold_url))
        {
            FileInfo fi = new FileInfo(filepath);
            fi.Delete();
        }
    }

    private void moviefile_delete()
    {
        string fold_url = "files/" + content;
        fold_url = System.Web.HttpContext.Current.Server.MapPath(fold_url);
        foreach (string filepath in Directory.GetFiles(fold_url))
        {
            FileInfo fi = new FileInfo(filepath);
            fi.Delete();
        }
    }

    private void pdffile_delete()
    {
        string fold_url = "files/" + content;
        fold_url = System.Web.HttpContext.Current.Server.MapPath(fold_url);
        foreach (string filepath in Directory.GetFiles(fold_url))
        {
            FileInfo fi = new FileInfo(filepath);
            fi.Delete();
        }
    }
}
